﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.WorldArtPrice.Entities;
using System.Data.SqlClient;
using System.Data;

namespace BusinessLayer.WorldArtPrice
{
    public class MenuBL
    {
        private readonly DbHelper _db;

        public MenuBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);         
        }

        public Menu GetById(int id)
        {
            var oCommand = new SqlCommand("sp_Menu_Select_By_Id") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));

            var list = _db.GetList<Menu>(oCommand);
            return list.FirstOrDefault();
        }

        public List<Menu> GetAll()
        {
            var oCommand = new SqlCommand("sp_Menu_Select_All") { CommandType = CommandType.StoredProcedure };            

            var list = _db.GetList<Menu>(oCommand);
            return list;
        }

        public void Insert(Menu menu)
        {
            var oCommand = new SqlCommand("sp_Menu_Insert") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@MenuName", menu.MenuName));
            oCommand.Parameters.Add(new SqlParameter("@Order", menu.Order));
            oCommand.Parameters.Add(new SqlParameter("@RedirectUrl", menu.RedirectUrl));
            oCommand.Parameters.Add(new SqlParameter("@Status", menu.Status));
            _db.ExecuteNonQuery(oCommand);            
        }

        public void Update(Menu menu)
        {
            var oCommand = new SqlCommand("sp_Menu_Update") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", menu.Id));
            oCommand.Parameters.Add(new SqlParameter("@MenuName", menu.MenuName));
            oCommand.Parameters.Add(new SqlParameter("@Order", menu.Order));
            oCommand.Parameters.Add(new SqlParameter("@RedirectUrl", menu.RedirectUrl));
            oCommand.Parameters.Add(new SqlParameter("@Status", menu.Status));
            _db.ExecuteNonQuery(oCommand);
        }

        public void Delete(int id)
        {
            var oCommand = new SqlCommand("sp_Menu_Delete") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            _db.ExecuteNonQuery(oCommand);
        }
    }
}
